#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "librarymanagment.h"

#include <QString>
#include <QDebug>
#include <QMessageBox>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include <QPixmap>
#include <QPalette>
#include <QBitmap>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //---------设置背景---------------------------
    ui->setupUi(this);
    // D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//xiyang.jpg
    // D://Qt//workspace//bookManager//pictures//xiyang.jpg
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//flower.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);
    //--------------------------------------------

//文件流分两种：文本流、数据流。
/* 文本流可以处理int,float,string,要#include <QTextStream>是个轻量级的方式
   数据流可以处理所有东西，且可以在任何平台上安全打开*/


    //第一步；创建文件对象
    QFile file("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//suoyoushuju.txt");
    //QFile file("D://Qt//workspace//bookManager//suoyoushuju.txt");
    //第二步：指定打开方式
    int i = 0;
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
    //第三步：读文件：①readAll,返回一个字节数组
        QTextStream in(&file);
//        in.setCodec("utf8");


        while(!in.atEnd()){
            QString line = in.readLine();//.txt文件内容最后一行，鼠标光标就停留在最后一行，千万不要换行，不然readLine会读取空行，然后就崩了
//            qDebug()<<"这是测试："<<line;
            line = line.trimmed();
            QStringList aPeiceOfInfo = line.split(" ");
            //------这是测试-------------
            qDebug()<<"QList的size：" <<aPeiceOfInfo.size();
            //--------------------------
            dataArray[i].isbn = aPeiceOfInfo[0];
            dataArray[i].bookTitle = aPeiceOfInfo[1];
            dataArray[i].author = aPeiceOfInfo[2];
            dataArray[i].publisher = aPeiceOfInfo[3];
            dataArray[i].dataAdded = aPeiceOfInfo[4];
            dataArray[i].qtyOnHand = aPeiceOfInfo[5];
            dataArray[i].wholesale = aPeiceOfInfo[6];
            dataArray[i].retail = aPeiceOfInfo[7];
            if(aPeiceOfInfo[8] == "1") dataArray[i].existOrNot = true;
            else {dataArray[i].existOrNot = false;}

            dataArray[i].tex = "0.06";

            //----------------测试-----------
            QString cnt = dataArray[i].isbn + " " + dataArray[i].bookTitle + " " + dataArray[i].author + " " + dataArray[i].publisher + " " + dataArray[i].dataAdded
                    + " " + dataArray[i].qtyOnHand + " " + dataArray[i].wholesale + " " + dataArray[i].retail /*+ " " + dataArray[i].existOrNot*/;
            qDebug()<<cnt;
            //------------------------------

            i++;

//            book_lines.append(line);
        }
        file.close();

        }else{
        QMessageBox::critical(this,"错误","文件打开失败，没有将信息导入对象数组！","确定");
    }






/*测试数据 */
//    dataArray[0].isbn = "161910218";dataArray[0].bookTitle = "数据结构";dataArray[0].author = "罗小黑";dataArray[0].publisher = "清华大学出版社";
//    dataArray[0].dataAdded = "2012-07-15";dataArray[0].qtyOnHand = "60";dataArray[0].wholesale = "23.5";dataArray[0].retail = "45.1";dataArray[0].existOrNot = true;

//    dataArray[1].isbn = "786767";dataArray[1].bookTitle = "工科数学";dataArray[1].author = "小红";dataArray[1].publisher = "机械出版社";
//    dataArray[1].dataAdded = "2013-03-12";dataArray[1].qtyOnHand = "8776";dataArray[1].wholesale = "52.6";dataArray[1].retail = "100.2";dataArray[1].existOrNot = true;

//    dataArray[2].isbn = "23232323";dataArray[2].bookTitle = "语文";dataArray[2].author = "让大哥";dataArray[2].publisher = "电饭锅蛋糕";
//    dataArray[2].dataAdded = "2001-02-11";dataArray[2].qtyOnHand = "7676";dataArray[2].wholesale = "45.5";dataArray[2].retail = "78";dataArray[2].existOrNot = true;

//    dataArray[3].isbn = "74444444";dataArray[3].bookTitle = "物理";dataArray[3].author = "发给";dataArray[3].publisher = "统一集团已经";
//    dataArray[3].dataAdded = "2005-06-23";dataArray[3].qtyOnHand = "4243";dataArray[3].wholesale = "58.6";dataArray[3].retail = "120.1";dataArray[3].existOrNot = true;

//    dataArray[4].isbn = "838383";dataArray[4].bookTitle = "化学";dataArray[4].author = "小白";dataArray[4].publisher = "阿斯达所";
//    dataArray[4].dataAdded = "1988-05-12";dataArray[4].qtyOnHand = "453453";dataArray[4].wholesale = "12.5";dataArray[4].retail = "53.5";dataArray[4].existOrNot = true;

//    dataArray[5].isbn = "76333";dataArray[5].bookTitle = "大数据";dataArray[5].author = "小红";dataArray[5].publisher = "凤凰沟通";
//    dataArray[5].dataAdded = "1977-02-01";dataArray[5].qtyOnHand = "386386";dataArray[5].wholesale ="23.2";dataArray[5].retail = "78";dataArray[5].existOrNot = true;

//    dataArray[6].isbn = "45345377";dataArray[6].bookTitle = "男友力";dataArray[6].author = "张伟";dataArray[6].publisher = "爱情公寓";
//    dataArray[6].dataAdded = "2011-04-23";dataArray[6].qtyOnHand = "4949";dataArray[6].wholesale = "454";dataArray[6].retail = "79879";dataArray[6].existOrNot = true;

//    dataArray[7].isbn = "7896542";dataArray[7].bookTitle = "女友力哦";dataArray[7].author = "小黑黑";dataArray[7].publisher = "阿斯达所";
//    dataArray[7].dataAdded = "2005-05-05";dataArray[7].qtyOnHand = "453453";dataArray[7].wholesale = "12";dataArray[7].retail = "36";dataArray[7].existOrNot = true;

//    dataArray[8].isbn = "1233969";dataArray[8].bookTitle = "C语言";dataArray[8].author = "罗贯中";dataArray[8].publisher = "北京大学出版社";
//    dataArray[8].dataAdded = "2019-03-23";dataArray[8].qtyOnHand = "363";dataArray[8].wholesale = "43";dataArray[8].retail = "258";dataArray[8].existOrNot = true;

//    dataArray[9].isbn = "83883";dataArray[9].bookTitle = "JAVA";dataArray[9].author = "小兰";dataArray[9].publisher = "南京大学出版社";
//    dataArray[9].dataAdded = "2016-05-24";dataArray[9].qtyOnHand = "76786";dataArray[9].wholesale = "75.6";dataArray[9].retail = "100.1";dataArray[9].existOrNot = true;

//    dataArray[10].isbn = "13543";dataArray[10].bookTitle = "CSGO";dataArray[10].author = "腾讯";dataArray[10].publisher = "腾讯出版社";
//    dataArray[10].dataAdded = "2012-12-12";dataArray[10].qtyOnHand = "453453";dataArray[10].wholesale = "8";dataArray[10].retail = "20";dataArray[10].existOrNot = true;

//    dataArray[11].isbn = "75327ghj";dataArray[11].bookTitle = "英雄魂";dataArray[11].author = "大三国时代";dataArray[11].publisher = "对方";
//    dataArray[11].dataAdded = "2015-09-30";dataArray[11].qtyOnHand = "723";dataArray[11].wholesale = "36";dataArray[11].retail = "45";dataArray[11].existOrNot = true;

//    dataArray[12].isbn = "17174";dataArray[12].bookTitle = "数学建模";dataArray[12].author = "哈哈";dataArray[12].publisher = "撒到出版社";
//    dataArray[12].dataAdded = "2001-08-29";dataArray[12].qtyOnHand = "3687";dataArray[12].wholesale = "78";dataArray[12].retail = "234";dataArray[12].existOrNot = true;

//    dataArray[13].isbn = "7687sdfs";dataArray[13].bookTitle = "China";dataArray[13].author = "fucj";dataArray[13].publisher = "如若他出版社";
//    dataArray[13].dataAdded = "2014-05-14";dataArray[13].qtyOnHand = "7896354";dataArray[13].wholesale = "56";dataArray[13].retail = "8689";dataArray[13].existOrNot = true;

//    dataArray[14].isbn = "1234zghf";dataArray[14].bookTitle = "欧赔";dataArray[14].author = "高合金钢";dataArray[14].publisher = "法国灰色出版社";
//    dataArray[14].dataAdded = "2013-05-25";dataArray[14].qtyOnHand = "63178";dataArray[14].wholesale = 54;dataArray[14].retail = "98";dataArray[14].existOrNot = true;

//    dataArray[15].isbn = "78978gfh";dataArray[15].bookTitle = "拉四季度";dataArray[15].author = "围绕";dataArray[15].publisher = "VC吧出版社";
//    dataArray[15].dataAdded = "1955-05-26";dataArray[15].qtyOnHand = "45693";dataArray[15].wholesale = "62";dataArray[15].retail = "780";dataArray[15].existOrNot = true;

//    dataArray[16].isbn = "67867fgha";dataArray[16].bookTitle = "汉尼拔";dataArray[16].author = "自行车";dataArray[16].publisher = "VB对方出版社";
//    dataArray[16].dataAdded = "2017-11-09";dataArray[16].qtyOnHand = "87967";dataArray[16].wholesale = "23";dataArray[16].retail = "78";dataArray[16].existOrNot = true;

//    dataArray[17].isbn = "72ghgj";dataArray[17].bookTitle = "粑粑";dataArray[17].author = "艰苦";dataArray[17].publisher = "人员出版社";
//    dataArray[17].dataAdded = "2013-07-29";dataArray[17].qtyOnHand = "1238";dataArray[17].wholesale = "52";dataArray[17].retail = "96";dataArray[17].existOrNot = true;

//    dataArray[18].isbn = "556877";dataArray[18].bookTitle = "哈萨克回复";dataArray[18].author = "染发";dataArray[18].publisher = "爱神的出版社";
//    dataArray[18].dataAdded = "2015-03-26";dataArray[18].qtyOnHand = "8787";dataArray[18].wholesale = "456";dataArray[18].retail = "89";dataArray[18].existOrNot = true;

//    dataArray[19].isbn = "36877dfg";dataArray[19].bookTitle = "写围绕";dataArray[19].author = "是东方红";dataArray[19].publisher = "地区和我出版社";
//    dataArray[19].dataAdded = "2018-08-27";dataArray[19].qtyOnHand = "4162298";dataArray[19].wholesale = "457";dataArray[19].retail = "56";dataArray[19].existOrNot = true;

//    dataArray[20].isbn = "45342";dataArray[20].bookTitle = "拉拉手的";dataArray[20].author = "于一体";dataArray[20].publisher = "丑八怪出版社";
//    dataArray[20].dataAdded = "2013-03-04";dataArray[20].qtyOnHand = "998975";dataArray[20].wholesale = "23";dataArray[20].retail = "20";dataArray[20].existOrNot = true;

//    dataArray[21].isbn = "71135";dataArray[21].bookTitle = "黑哦";dataArray[21].author = "热";dataArray[21].publisher = "玩儿出版社";
//    dataArray[21].dataAdded = "1999-12-25";dataArray[21].qtyOnHand = "742";dataArray[21].wholesale = "75";dataArray[21].retail = "20";dataArray[21].existOrNot = true;

//    for(;i<100;i++){
//        dataArray[i].isbn = "\0";
//        dataArray[i].bookTitle = "\0";
//        dataArray[i].author = "\0";
//        dataArray[i].publisher = "\0";
//        dataArray[i].dataAdded = "\0";
//        dataArray[i].qtyOnHand = "0";
//        dataArray[i].wholesale = "\0";
//        dataArray[i].retail = "\0";
//        dataArray[i].existOrNot = false;
//    }
//    for(int i = 0;i < 100;i++){
//        if(i <= 21){
//            dataArray[i].tex = "0.06";
//        }else{
//            dataArray[i].tex = "\0";
//        }
//    }


}

MainWindow::~MainWindow()
{

    delete ui;
}

void MainWindow::on_pushButton_clicked()
{


    QString input = this->ui->le_mainInput->text();
    int in = input.toInt();

    if(in < 1 || in > 3)
    {
        QMessageBox::critical(this,"错误","请输入1-3之间的数字！","确定");
    }else{
        switch (in) {
        case 1:
            this->c.show();
            break;
        case 2:
            this->a.show();
            break;
        case 3:
            this->s.show();
            break;
//        case 4:
//            this->close();
//            break;
        default:
            break;
        }

     }

}

void MainWindow::on_pushButton_2_clicked()
{

    //QFile file("D://Qt//workspace//bookManager//suoyoushuju.txt");
    QFile file("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//suoyoushuju.txt");

    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream out(&file);

        for(int i = 0;i < 100;i++){
            if(dataArray[i].existOrNot){
                out << dataArray[i].isbn<<" "<<dataArray[i].bookTitle <<" "<<dataArray[i].author<<" "<<dataArray[i].publisher<<" "
                    <<dataArray[i].dataAdded<<" "<<dataArray[i].qtyOnHand<<" "<<dataArray[i].wholesale<<" "<<dataArray[i].retail<<" "<<"1"<<endl;
            }
        }
        file.close();
        qDebug()<<"成功写入文件！";

    }else{
        QMessageBox::critical(this,"错误","文件打开失败，没有将数组信息导入文件！","确定");
    }

    this->close();
}
